import { LoginInfo } from "maishu-services-sdk";
import { LocalValueStore, AjaxOptions, Service } from "maishu-chitu-service";
import { Service as BaseLocalService } from "maishu-chitu-admin/static";
import md5 = require("js-md5");

export class LocalService extends BaseLocalService {
    loginInfo = new LocalValueStore<LoginInfo>("login-info");

    ajax<T>(url: string, options: AjaxOptions) {
        options.headers = options.headers || {};
        if (this.loginInfo.value)
            options.headers["token"] = this.loginInfo.value.token;

        return super.ajax<T>(url, options);
    }

    async login(username: string, password: string) {
        password = md5(password);
        let url = this.localUrl("login")
        let r = await this.postByJson<LoginInfo>(url, { username, password });
        this.loginInfo.value = r;
        return r;
    }
}